package com.cxmx.escort.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxmx.escort.base.BaseController;
import com.cxmx.escort.base.BasePage;
import com.cxmx.escort.base.ResponseFormat;
import com.cxmx.escort.base.Result;
import com.cxmx.escort.entity.Bed;
import com.cxmx.escort.entity.BedOrder;
import com.cxmx.escort.entity.DTO.SalesmanLogin;
import com.cxmx.escort.entity.Region;
import com.cxmx.escort.entity.Salesman;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 业务员 前端控制器
 * </p>
 *
 * @author 陈云轩
 * @since 2019-10-21
 */
@RestController
@RequestMapping("/salesman")
@Api(value = " 用户", tags = {" 用户操作接口"})
@Validated
@SuppressWarnings("unchecked")
public class SalesmanController extends BaseController {
    @ApiOperation(value = "业务员登录", httpMethod = "POST")
    @PostMapping
    public Result<String> salesmanLogin(@RequestBody @ApiParam(name = "登录") SalesmanLogin login) {
        QueryWrapper<Salesman> qw = new QueryWrapper<>();
        qw.eq("mobile", login.getMobile());
        qw.eq("password", login.getPassword());
        Salesman user = salesmanService.getOne(qw);
        if (null == user) {
            return ResponseFormat.retParam(20004, null);
        } else {
            return ResponseFormat.retParam(200, user.getId());
        }
    }

    @ApiOperation(value = "代理商所有的医院病区", httpMethod = "GET")
    @GetMapping("hospital/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "代理商id", dataType = "String", paramType = "path", required = true),
    })
    public Result<Region> getPageForOne(@PathVariable("id") String id) {
        QueryWrapper<Region> qw = new QueryWrapper<Region>();
        qw.eq("salesman_id", id);
        List<Region> regions = regionService.list(qw);
        return ResponseFormat.retParam(200, regions);
    }

    @ApiOperation(value = "医院病区下的床", httpMethod = "GET")
    @GetMapping("bed/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "病区id", dataType = "String", paramType = "path", required = true),
    })
    public Result<Bed> get(@PathVariable("id") String id) {
        QueryWrapper<Bed> qw = new QueryWrapper<Bed>();
        qw.eq("hospital_id", id);
        List<Bed> regions = bedService.list(qw);
        return ResponseFormat.retParam(200, regions);
    }

    @ApiOperation(value = "床的订单", httpMethod = "GET")
    @GetMapping("bedOrder/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "床id", dataType = "String", paramType = "path", required = true),
    })
    public Result<BedOrder> getBedOrder(@PathVariable("id") String id) {
        QueryWrapper<BedOrder> qw = new QueryWrapper<BedOrder>();
        qw.eq("bed_id", id);
        qw.eq("order_status", 3);
        List<BedOrder> regions = bedOrderService.list(qw);
        return ResponseFormat.retParam(200, regions);
    }

    @ApiOperation(value = "时间段订单列表", httpMethod = "GET")
    @GetMapping("/{pageNum}/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "String", name = "pageNum", value = "分页", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "pageSize", value = "每一页条数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id", value = "代理商id"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endTime", value = "结束时间"),
    })
    public Result<BasePage<BedOrder>> getPage(@NotNull(message = "pageNum不能为空") @Min(value = 1, message = "分页最小为1") @PathVariable("pageNum") Integer pageNum,
                                              @NotNull(message = "pageSize不能为空") @Max(value = 20, message = "每页最大20") @PathVariable("pageSize") Integer pageSize,
                                              String id,
                                              String beginTime,
                                              String endTime

    ) {
        QueryWrapper<BedOrder> qw = new QueryWrapper<>();
        if (id != null && !"".equals(id)) {
            qw.eq("a.salesman_id", id);
        }
        if (beginTime != null && !"".equals(beginTime)) {
            beginTime = beginTime + " 00:00:00";
            qw.apply("UNIX_TIMESTAMP(c.create_time) >= UNIX_TIMESTAMP('" + beginTime + "')");
        }
        if (endTime != null && !"".equals(endTime)) {
            endTime = endTime + " 23:59:59";
            qw.apply("UNIX_TIMESTAMP(c.create_time) <= UNIX_TIMESTAMP('" + endTime + "')");
        }
        Page<BedOrder> page = new Page<>(pageNum, pageSize);
        qw.eq("c.order_status", 3);
        qw.eq("a.delete_flag", 0);
        qw.orderByDesc("c.create_time");
        IPage<BedOrder> roleDOList = bedOrderService.pageOrder(page, qw);
        return ResponseFormat.retParam(200, roleDOList);
    }

    @ApiOperation(value = "时间段订单总额", httpMethod = "GET")
    @GetMapping("count")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id", value = "代理商id"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endTime", value = "结束时间"),
    })
    public Result<Double> getBedOrder(String id,
                                         String beginTime,
                                         String endTime) {
        QueryWrapper<BedOrder> qw = new QueryWrapper<>();
        if (id != null && !"".equals(id)) {
            qw.eq("a.salesman_id", id);
        }
        if (beginTime != null && !"".equals(beginTime)) {
            beginTime = beginTime + " 00:00:00";
            qw.apply("UNIX_TIMESTAMP(c.create_time) >= UNIX_TIMESTAMP('" + beginTime + "')");
        }
        if (endTime != null && !"".equals(endTime)) {
            endTime = endTime + " 23:59:59";
            qw.apply("UNIX_TIMESTAMP(c.create_time) <= UNIX_TIMESTAMP('" + endTime + "')");
        }
        qw.eq("c.order_status", 3);
        qw.eq("a.delete_flag", 0);
        qw.orderByDesc("c.create_time");
        Double countPrice = bedOrderService.countPrice(qw);
        return ResponseFormat.retParam(200, countPrice);
    }

}

